<template>
  <el-button
    type="primary"
    size="small"
    :icon="ChatDotRound"
    @click="startChat"
  >
    发起聊天
  </el-button>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ChatDotRound } from '@element-plus/icons-vue'
import { getOrCreateSession } from '@/api/chat'

const props = defineProps({
  userId: {
    type: Number,
    required: true
  }
})

const router = useRouter()

const startChat = async () => {
  try {
    await getOrCreateSession(props.userId)
    router.push('/chat')
    ElMessage.success('正在打开聊天窗口...')
  } catch (error) {
    ElMessage.error('打开聊天窗口失败')
  }
}
</script>

